<?php

namespace App\Admin\Controllers;

use App\UserPrize;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\User;

class UserPrizeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '中奖列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserPrize());

        $grid->column('id', __('Id'))->sortable();;
        $grid->column('user_id', __('用户id'));
        $grid->column('user_name',__('用户昵称'))->display(function () {
            $res = User::find($this->user_id);
            if ($res){
                $name = $res->name;
            }else{
                $name ="";
            }

            return $name;
        });
        $grid->column('price', __('金额'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('get_date', __('兑奖期限'))->sortable();;

        $grid->model()->latest();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserPrize::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('用户id'));
        $show->field('price', __('金额'));
        $show->field('created_at', __('创建时间'));
        $show->field('get_date', __('兑奖期限'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new UserPrize());

        $form->number('user_id', __('用户id'));
        $form->text('price', __('金额'));
        $form->date('get_date', __('兑奖期限'))->default(date('Y-m-d'));

        return $form;
    }
}
